#   Copyright (c)  2021 FromVB2Python
#   FromVB2Python is licensed under Mulan PSL v2.
#   You can use this software according to the terms and conditions of the Mulan PSL v2. 
#   You may obtain a copy of Mulan PSL v2 at:
#            http://license.coscl.org.cn/MulanPSL2 
#   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
#   See the Mulan PSL v2 for more details.
#   048-学考-三角形统计-201902-14温州选考模拟 GUI版本
import tkinter as tk
win = tk.Tk()
win.title('三角形统计')
#函数
def tongji():
    num = 0
    a = text.get('1.0','end').split(',')
    k = a[:len(a)-1]#去掉末尾\n
    a_list = [] #整理用，去掉多余的'\n'和 ''
    c_list = [] #用来放能够形成三角形的
    #注意末尾加逗号，不然会被过滤掉
    for i in range(len(k)):
        try:
            if float(a[i]): 
                a_list.append(a[i])
        except:
            pass
    #切割分块
    b_list = [a_list[i:i+3] for i in range(0, len(a_list),3)]
    for i in range(len(b_list)):
        if len(b_list[i])==3:#长度为3的进行判断是否构成三角形
            #print('长度是3的',b_list[i],i)
            a = float(b_list[i][0]) #边a
            b = float(b_list[i][1]) #边b
            c = float(b_list[i][2]) #边c
            if a+b>c and a+c>b and b+c>a: #任意两边之和大于第三边
                num = num+1
                c_list.append(b_list[i])
    label1['text'] = '一共包含了' + str(num) + '个三角形'
    label2['text'] = c_list
    
    
#定义
text = tk.Text(win, height = 5, width = 30)
btn = tk.Button(win, text = '统计',width = 10, height = 3,command = tongji)
label1 = tk.Label(win)
label2 = tk.Label(win)
#位置
text.grid(row = 0, column = 0, columnspan = 2,
          padx = 10, pady = 10)
btn.grid(row = 0, column = 2,
         padx = 10, pady = 10)
label1.grid(row = 1, column = 0,
            padx = 10, pady = 10, sticky = tk.E)
label2.grid(row = 2, column = 0, columnspan = 2,
            padx = 10, pady = 10, sticky = tk.W)

win.mainloop()
